using System;
using System.Data;
using System.Data.SqlClient;

using Sunyi.Project.WebPoint.Common;
using Sunyi.Project.WebPoint.DataAccess;

using ActivitySystem.DataContract;

namespace ActivitySystem.DataAccess
{
    public partial class ParticipationDao : IDao<Participation>
    {
        public Participation Select(Guid id)
        {
            Participation participation = null;

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_Participation_Select";
            command.Parameters.Add(new SqlParameter("@Id", id));

            try
            {
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        participation = CreateEntityByReader(reader);
                    }
                }
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }

            return participation;
        }

        public void Insert(Participation participation)
        {
            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_Participation_Insert";
            command.Parameters.Add(new SqlParameter("@Id", participation.Id));
            command.Parameters.Add(new SqlParameter("@UserId", participation.UserId));
            command.Parameters.Add(new SqlParameter("@ActivityId", participation.ActivityId));
            command.Parameters.Add(new SqlParameter("@Fellow", participation.Fellow));
            command.Parameters.Add(new SqlParameter("@Comments", participation.Comments));
            command.Parameters.Add(new SqlParameter("@IsApproved", participation.IsApproved));
            command.Parameters.Add(new SqlParameter("@IsActive", participation.IsActive));
            command.Parameters.Add(new SqlParameter("@InsertTimeStamp", participation.InsertTimeStamp));
            command.Parameters.Add(new SqlParameter("@UpdateTimeStamp", participation.UpdateTimeStamp));

            foreach (SqlParameter parameter in command.Parameters)
            {
                if (parameter.Value == null || parameter.Value.ToString() == Guid.Empty.ToString()) parameter.Value = DBNull.Value;
            }

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        public void Update(Participation participation)
        {
            if (participation.UpdateTimeStamp == DateTime.MinValue) participation.UpdateTimeStamp = DateTime.Now;

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_Participation_Update";
            command.Parameters.Add(new SqlParameter("@Id", participation.Id));
            command.Parameters.Add(new SqlParameter("@UserId", participation.UserId));
            command.Parameters.Add(new SqlParameter("@ActivityId", participation.ActivityId));
            command.Parameters.Add(new SqlParameter("@Fellow", participation.Fellow));
            command.Parameters.Add(new SqlParameter("@Comments", participation.Comments));
            command.Parameters.Add(new SqlParameter("@IsApproved", participation.IsApproved));
            command.Parameters.Add(new SqlParameter("@IsActive", participation.IsActive));
            command.Parameters.Add(new SqlParameter("@InsertTimeStamp", participation.InsertTimeStamp));
            command.Parameters.Add(new SqlParameter("@UpdateTimeStamp", participation.UpdateTimeStamp));

            foreach (SqlParameter parameter in command.Parameters)
            {
                if (parameter.Value == null || parameter.Value.ToString() == Guid.Empty.ToString()) parameter.Value = DBNull.Value;
            }

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        public void Delete(Guid id)
        {
            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_Participation_Delete";
            command.Parameters.Add(new SqlParameter("@Id", id));

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        #region Private Method

        internal static Participation CreateEntityByReader(IDataReader reader)
        {
            Participation participation = new Participation();
            if (reader["Id"] != DBNull.Value) participation.Id = (Guid)reader["Id"];
            if (reader["UserId"] != DBNull.Value) participation.UserId = (Guid)reader["UserId"];
            if (reader["ActivityId"] != DBNull.Value) participation.ActivityId = (Guid)reader["ActivityId"];
            if (reader["Fellow"] != DBNull.Value) participation.Fellow = (Int32)reader["Fellow"];
            if (reader["Comments"] != DBNull.Value) participation.Comments = reader["Comments"] as string;
            if (reader["IsApproved"] != DBNull.Value) participation.IsApproved = (Boolean)reader["IsApproved"];
            if (reader["IsActive"] != DBNull.Value) participation.IsActive = (Boolean)reader["IsActive"];
            if (reader["InsertTimeStamp"] != DBNull.Value) participation.InsertTimeStamp = (DateTime)reader["InsertTimeStamp"];
            if (reader["UpdateTimeStamp"] != DBNull.Value) participation.UpdateTimeStamp = (DateTime)reader["UpdateTimeStamp"];

            return participation;
        }

        #endregion
    }
}